const db = require('../db/index')
//处理密码模块
const bcrypt = require('bcryptjs')
//获取用户信息的路由
exports.getUserInfo = (req, res) => {
  let sql = ''
  switch (req.auth.identity) {
    case '学生':
      sql = `select id,name,sex,school,major,classid from student where id=?`
      break
    case '教师':
      sql = `select id,name,sex,school,department from teacher where id=?`
      break
    case '管理员':
      sql = `select id,name,sex,school from administrator where id=?`
      break
  }
  db.query(sql, req.auth.id, (err, results) => {
    if (err) {
      return res.cc(err)
    }
    if (!results.length) {
      return res.cc('获取用户信息失败！')
    }
    res.send({
      status: 0,
      message: '获取用户信息成功！',
      data: results[0]
    })
  })
}
//获取所有用户信息路由
exports.getAllUserInfo = (req, res) => {
  let sql = ''
  switch (req.query.identity) {
    case '学生':
      sql = `select id,name,sex,school,major,classid from student`
      break
    case '教师':
      sql = `select id,name,sex,school,department from teacher`
      break
    case '管理员':
      sql = `select id,name,sex,school from administrator`
      break
  }
  db.query(sql, (err, results) => {
    if (err) {
      return res.cc(err)
    }
    if (!results.length) {
      return res.cc('获取用户信息失败！')
    }
    res.send({
      status: 0,
      message: '获取用户信息成功！',
      data: results
    })
  })
}
//更新用户信息的路由
exports.updateUserInfo = (req, res) => {
  let sql = ''
  switch (req.body.status) {
    case '学生':
      sql = `update student set ? where id=?`
      break
    case '教师':
      sql = `update teacher set ? where id=?`
      break
    case '管理员':
      sql = `update administrator set ? where id=?`
      break
  }
  delete req.body.status
  db.query(sql, [req.body, req.body.id], (err, results) => {
    if (err) {
      return res.cc(err)
    }
    if (results.affectedRows !== 1) {
      return res.cc('修改信息失败！')
    }
    res.cc('修改信息成功！', 0)
  })
}
//更新用户密码的路由
exports.updatePassword = (req, res) => {
  let identity = ''
  switch (req.auth.identity) {
    case '学生':
      identity = 'student'
      break
    case '教师':
      identity = 'teacher'
      break
    case '管理员':
      identity = 'administrator'
      break
  }
  const sql = `select * from ${identity} where id=?`
  db.query(sql, req.auth.id, (err, results) => {
    //执行sql语句失败
    if (err) {
      return res.cc(err)
    }
    if (results.length !== 1) {
      return res.cc('用户不存在！')
    }
    const flag = bcrypt.compareSync(req.body.oldPwd, results[0].password)
    if (!flag) {
      return res.cc('旧密码错误！')
    }
    const sql = `update ${identity} set password=? where id=?`
    const newPwd = bcrypt.hashSync(req.body.newPwd, 12)
    db.query(sql, [newPwd, req.auth.id], (err, results) => {
      if (err) {
        return res.cc(err)
      }
      if (results.affectedRows !== 1) {
        return res.cc('更新密码失败！')
      }
      res.cc('更新密码成功！', 0)
    })
  })
}
exports.deleteuser = (req, res) => {
  let sql = ''
  switch (req.body.status) {
    case '学生':
      sql = `delete from student where id=?`
      break
    case '教师':
      sql = `delete from teacher where id=?`
      break
    case '管理员':
      sql = `delete from administrator where id=?`
      break
  }
  delete req.body.status
  db.query(sql, req.body.id, (err, results) => {
    if (err) {
      return res.cc(err)
    }
    if (results.affectedRows !== 1) {
      return res.cc('删除用户失败！')
    }
    res.cc('删除用户成功！', 0)
  })
}